{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sorting the Sentence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: sortSentence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #将句子排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>一个 <strong>句子</strong> 指的是一个序列的单词用单个空格连接起来，且开头和结尾没有任何空格。每个单词都只包含小写或大写英文字母。</p>\n",
    "\n",
    "<p>我们可以给一个句子添加 <strong>从 1 开始的单词位置索引 </strong>，并且将句子中所有单词 <strong>打乱顺序</strong> 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>比方说，句子 <code>\"This is a sentence\"</code> 可以被打乱顺序得到 <code>\"sentence4 a3 is2 This1\"</code> 或者 <code>\"is2 sentence4 This1 a3\"</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你一个 <strong>打乱顺序</strong> 的句子 <code>s</code> ，它包含的单词不超过 <code>9</code> 个，请你重新构造并得到原本顺序的句子。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>s = \"is2 sentence4 This1 a3\"\n",
    "<b>输出：</b>\"This is a sentence\"\n",
    "<b>解释：</b>将 s 中的单词按照初始位置排序，得到 \"This1 is2 a3 sentence4\" ，然后删除数字。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>s = \"Myself2 Me1 I4 and3\"\n",
    "<b>输出：</b>\"Me Myself and I\"\n",
    "<b>解释：</b>将 s 中的单词按照初始位置排序，得到 \"Me1 Myself2 and3 I4\" ，然后删除数字。</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 <= s.length <= 200</code></li>\n",
    "\t<li><code>s</code> 只包含小写和大写英文字母、空格以及从 <code>1</code> 到 <code>9</code> 的数字。</li>\n",
    "\t<li><code>s</code> 中单词数目为 <code>1</code> 到 <code>9</code> 个。</li>\n",
    "\t<li><code>s</code> 中的单词由单个空格分隔。</li>\n",
    "\t<li><code>s</code> 不包含任何前导或者后缀空格。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sorting-the-sentence](https://leetcode.cn/problems/sorting-the-sentence/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sorting-the-sentence](https://leetcode.cn/problems/sorting-the-sentence/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"is2 sentence4 This1 a3\"', '\"Myself2 Me1 I4 and3\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        ln = len(s)\n",
    "        ret = [''] * ln\n",
    "        for i in s:\n",
    "            ret[int(i[-1]) - 1] = i[:-1]\n",
    "        return ' '.join(ret)\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        list1 = s.split(' ')\n",
    "        list2 = ['']*len(list1)\n",
    "        for i in list1:\n",
    "            list2[int(i[-1])-1] = i[:-1]\n",
    "        return ' '.join(list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        ls = s.split(' ')\n",
    "        res = [0]*len(ls)\n",
    "        for i in ls:\n",
    "            res[int(i[-1])-1] = i[:-1]\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s=s.split()\n",
    "        ans=[]\n",
    "        for j in range(len(s)):\n",
    "            for i in s:\n",
    "                if (j+1) == int(i[-1]):\n",
    "                    ans.append(i[0:-1])\n",
    "        return ' '.join(ans)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        r = ['']*len(s)\n",
    "        for i in s:\n",
    "            r[int(i[-1])-1]=i[:-1]\n",
    "        return ' '.join(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        sl=s.split()\n",
    "        r=[None for _ in range(len(sl))]\n",
    "        for i in sl:\n",
    "            for j in range(len(i)):\n",
    "                if i[j].isdigit():\n",
    "                    idx=int(i[int(j):])-1\n",
    "                    #print(idx,i[:int(j)])\n",
    "                    r[idx]=i[:int(j)]\n",
    "                    break\n",
    "        #print(r)\n",
    "        return ' '.join(r)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        n = len(s)   # 单词数量\n",
    "        arr = [\"\" for _ in range(n)]   # 单词数组\n",
    "        for wd in s:\n",
    "            # 计算位置索引对应的单词数组下标，并将单词放入对应位置\n",
    "            # 数组下标为 0 开头，位置索引为 1 开头\n",
    "            arr[int(wd[-1])-1] = wd[:-1]\n",
    "        return \" \".join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        my_str = s.split(' ')\n",
    "        re_str = ''\n",
    "        for i in range(1, len(my_str)+1):\n",
    "            for j in my_str:\n",
    "                if str(i) in j :\n",
    "                    re_str = re_str + ' ' + j[:-1]\n",
    "                    break\n",
    "        return re_str[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words = s.split(' ')\n",
    "        sentence = {}\n",
    "        for word in words:\n",
    "            sentence[word[-1]] = word[:-1]\n",
    "        return ' '.join([sentence[str(i+1)] for i in range(len(words))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        result = []\n",
    "        for i in range(1,len(s.split(' '))+1):\n",
    "            for word in s.split(' '):\n",
    "                if eval(word[-1]) == i:\n",
    "                    result.append(word[:-1])\n",
    "        return ' '.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        sentence = []\n",
    "        i = 0\n",
    "        n = len(s)\n",
    "        while i < n:\n",
    "            temp = ''\n",
    "            while s[i].isalpha():\n",
    "                temp += s[i]\n",
    "                i += 1\n",
    "            sentence.append([temp, s[i]])\n",
    "            i += 2\n",
    "        res, _ = zip(*sorted(sentence, key=lambda x: x[1]))\n",
    "        return ' '.join(res)\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        li = list(s.split( ))\n",
    "        li = sorted(li,key=lambda x: x[-1])\n",
    "        return ' '.join(i[:-1] for i in li)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        n = len(s)\n",
    "        temp = ['' for _ in range(n)]\n",
    "        for item in s:\n",
    "            temp[int(item[-1])-1] = item[:-1]\n",
    "        return \" \".join(temp)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        ss = s.split()\n",
    "        ans = [\"\"]*len(ss)\n",
    "        for i in range(len(ss)):\n",
    "            ans[int(ss[i][-1])-1] = ss[i][:-1]\n",
    "        return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        ss = s.split(\" \")\n",
    "        res = [\"\"] * len(ss)\n",
    "        for x in ss:\n",
    "            res[int(x[-1])-1] = x[:-1]\n",
    "        return \" \".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split(' ')\n",
    "        for i in range(len(s)):\n",
    "            for j in range(i+1,len(s)):\n",
    "                if s[i][-1]> s[j][-1]:\n",
    "                    s[i],s[j]=s[j],s[i]\n",
    "        for i in range(len(s)):\n",
    "            s[i]=s[i][:-1]\n",
    "        return ' '.join(list(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        s.sort(key=lambda x:x[-1])\n",
    "        return ' '.join(x[:-1] for x in s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s=s.split()\n",
    "        n=len(s)\n",
    "        arr=[\"\"for _ in range(n)]\n",
    "        for wd in s:\n",
    "            arr[int(wd[-1])-1]=wd[:-1]\n",
    "        return \" \".join(arr)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        arr = s.split(\" \")\n",
    "        res = ['' for _ in range(len(arr))]\n",
    "        for i in arr:\n",
    "            index = int(i[-1]) - 1\n",
    "            res[index] = i[:(len(i) - 1)]\n",
    "        return \" \".join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        n = len(s)   # 单词数量\n",
    "        arr = [\"\" for _ in range(n)]   # 单词数组\n",
    "        for wd in s:\n",
    "            # 计算位置索引对应的单词数组下标，并将单词放入对应位置\n",
    "            # 数组下标为 0 开头，位置索引为 1 开头\n",
    "            arr[int(wd[-1])-1] = wd[:-1]\n",
    "        return \" \".join(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        lst = s.split(\" \")\n",
    "        ans = [\"\"] * len(lst)\n",
    "        for n in lst:\n",
    "            ans[int(n[-1]) - 1] = n[:-1]\n",
    "        return \" \".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        n = len(s)   # 单词数量\n",
    "        arr = [\"\" for _ in range(n)]   # 单词数组\n",
    "        for wd in s:\n",
    "            # 计算位置索引对应的单词数组下标，并将单词放入对应位置\n",
    "            # 数组下标为 0 开头，位置索引为 1 开头\n",
    "            arr[int(wd[-1])-1] = wd[:-1]\n",
    "        return \" \".join(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        a = s.split(\" \")\n",
    "        result = [0 for i in range(len(a))]\n",
    "        \n",
    "        for i in range(len(a)):\n",
    "            result[int(a[i][-1])-1]=a[i][:-1]\n",
    "\n",
    "        \n",
    "        result = \" \".join(result)\n",
    "        print(result)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words=s.split(' ')\n",
    "        d={int(x[-1]):x[:-1] for x in words}\n",
    "        result=[]\n",
    "        for i in range(1,len(words)+1):\n",
    "            result.append(d[i])\n",
    "        return ' '.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        list1 = s.split(\" \")\n",
    "        list2 = [0 for i in range(len(list1))]\n",
    "        s = \"\"\n",
    "        for i in range(len(list1)):\n",
    "            list2[int(list1[i][-1])-1] = list1[i][:len(list1[i])-1]\n",
    "        for i in range(len(list2)):\n",
    "            if i==len(list2)-1:\n",
    "                s = s+list2[i]\n",
    "            else:\n",
    "                s = s+list2[i]+\" \"\n",
    "        return (s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        t=s.split(' ')\n",
    "        n=len(t)\n",
    "        res=['']*n\n",
    "        for word in t:\n",
    "            res[int(word[-1])-1]=word[0:len(word)-1]\n",
    "        ans=''\n",
    "        for i in range(n-1):\n",
    "            ans+=res[i]+' '\n",
    "        return ans+res[n-1]\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        li = [[word[-1], word[:-1]] for word in s.split()]\n",
    "        li.sort(key = lambda s : s[0])\n",
    "        return \" \".join([i[1] for i in li])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words = s.split(' ')\n",
    "        ans = ''\n",
    "        for i in range(len(words)):\n",
    "            for word in words:\n",
    "                if str(i + 1) in word:\n",
    "                    ans += word[:-1] + ' '\n",
    "        ans = ans[:-1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        ln = len(s)\n",
    "        ret = [''] * ln\n",
    "        for i in s:\n",
    "            ret[int(i[-1]) -1] = i[:-1]\n",
    "        return ' '.join(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        arr = [\"\" for _ in s]\n",
    "        for v in s:\n",
    "            arr[int(v[-1])-1] = v[:-1]\n",
    "        return \" \".join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words=s.split()\n",
    "        words=sorted(words, key=lambda w : w[-1])\n",
    "        return ' '.join(w[:-1] for w in words)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s1=s.split(' ')\n",
    "        s2=sorted(s1,key=lambda w :w[-1])\n",
    "        s3=\" \".join(x[:-1] for x in s2)\n",
    "    \n",
    "        return s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words=s.split(' ')\n",
    "        d={}\n",
    "        for word in words:\n",
    "            d[int(word[-1])]=word[:-1]\n",
    "        result=d[1]\n",
    "        for i in range(2,len(words)+1):\n",
    "            result+=' '+d[i]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        ls = s.split(' ')\n",
    "        ls.sort(key=lambda x:int(x[-1]))\n",
    "        for i in range(len(ls)):\n",
    "            ls[i] = ls[i][:-1]\n",
    "        return ' '.join(ls)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        t=s.split(' ')\n",
    "        x=[0]*len(t)\n",
    "        for i in t:\n",
    "            x[int(i[-1])-1]=i[:-1]\n",
    "        return ' '.join(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        resdict ={}\n",
    "        for v in s.split(' '):\n",
    "            resdict[v[-1]] = v[:-1]\n",
    "        resdict = dict(sorted(resdict.items(), key=lambda x: x[0]))\n",
    "        res =[]\n",
    "        for k in resdict.keys():\n",
    "            res.append(resdict[k])\n",
    "        return ' '.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s=s.split()\n",
    "        n=len(s)\n",
    "        arr=[\"\" for _ in range(n)]      #单词数组\n",
    "        for wd in s:                    #计算位置索引对应的单词数组下标，并将单词放入对应位置\n",
    "            arr[int(wd[-1])-1]=wd[:-1]\n",
    "        return \" \".join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        lst=s.split(' ')\n",
    "        str_lst=['' for i in range(len(lst))]\n",
    "        for string in lst:\n",
    "            str_lst[int(string[-1])-1]=string[:-1]\n",
    "        return ' '.join(str_lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s=s.split()\n",
    "        data=[\"\" for _ in range(len(s))]\n",
    "        for word in s:\n",
    "            data[int(word[-1])-1]=word[:-1]\n",
    "        return \" \".join(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        ans = [x[:len(x)-1] for x in sorted(s.split(' '),key = lambda x:x[-1])]\n",
    "        return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s=s.split()\n",
    "        ln=len(s)\n",
    "        res=['']*ln\n",
    "        for i in s:\n",
    "            res[int(i[-1])-1]=i[:-1]\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        sentence = []\n",
    "        i = 0\n",
    "        n = len(s)\n",
    "        while i < n:\n",
    "            temp = ''\n",
    "            while s[i].isalpha():\n",
    "                temp += s[i]\n",
    "                i += 1\n",
    "            sentence.append([temp, s[i]])\n",
    "            i += 2\n",
    "        res, _ = zip(*sorted(sentence, key=lambda x: x[1]))\n",
    "        return ' '.join(res)\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        word = s.split(' ')\n",
    "        word.sort(key=lambda x: x[-1])\n",
    "        ans = \" \".join(word)\n",
    "        ans = re.sub(r\"[0-9]\", \"\", ans)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        ans=''\n",
    "        l=s.split()\n",
    "        l.sort(key=lambda x:x[-1])\n",
    "        for i in l:\n",
    "            ans+=i[0:len(i)-1]\n",
    "            ans+=' '\n",
    "        return ans.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        l = s.split()\n",
    "        ans = [0] * len(l)\n",
    "        for w in l:\n",
    "            ans[int(w[-1]) - 1] = w[:-1]\n",
    "        return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        a = sorted(s.split(' '), key=lambda x: x[-1])\n",
    "        ans = []\n",
    "        for i in a:\n",
    "            ans.append(i[:-1])\n",
    "        return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        lst=s.split(' ')\n",
    "        str_lst=['' for i in range(len(lst))]\n",
    "        for string in lst:\n",
    "            str_lst[int(string[-1])-1]=string[:-1]\n",
    "        return ' '.join(str_lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s_=s.split(' ')\n",
    "        res_list=[]\n",
    "        d=dict()\n",
    "        for s__ in s_:\n",
    "            if s__[-1].isdigit():\n",
    "                d[int(s__[-1])]=s__\n",
    "        list_=sorted(d)\n",
    "        for key in list_:\n",
    "            res_list.append(d.get(key)[:-1:])\n",
    "        return ' '.join(res_list)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def sortSentence(self, s) :\n",
    "    t = s.split(' ')\n",
    "    n = len(t)\n",
    "\n",
    "    ans = [''] * n\n",
    "\n",
    "    for item in t:\n",
    "      idx = int(item[-1]) - 1\n",
    "      ans[idx] = item[:-1]\n",
    "\n",
    "    # print(ans)\n",
    "\n",
    "    return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        n = len(s)   # 单词数量\n",
    "        arr = [\"\" for _ in range(n)]   # 单词数组\n",
    "        for wd in s:\n",
    "            # 计算位置索引对应的单词数组下标，并将单词放入对应位置\n",
    "            # 数组下标为 0 开头，位置索引为 1 开头\n",
    "            arr[int(wd[-1])-1] = wd[:-1]\n",
    "        return \" \".join(arr)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s_lst = list(map(self.get_index, s.strip().split()))\n",
    "        #print(s_lst)\n",
    "        s_sorted = sorted(s_lst, key = lambda x : x[1])\n",
    "        return ' '.join([x[0] for x in s_sorted])\n",
    "\n",
    "    def get_index(self, s:str) -> list:\n",
    "        ans_str = ''\n",
    "        ans_index = 0\n",
    "        for x in s:\n",
    "            #print(x)\n",
    "            if x.isdigit():\n",
    "                ans_index = int(x)\n",
    "            else:\n",
    "                ans_str += x\n",
    "        return [ans_str, ans_index]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        start = 0\n",
    "        n = s.count(' ') + 1\n",
    "        print(n)\n",
    "        strArr = [None] * n\n",
    "        for i in range(len(s)):\n",
    "            if s[i].isdigit():\n",
    "                strArr[int(s[i]) - 1] = s[start:i]\n",
    "                start = i + 2\n",
    "        \n",
    "        return ' '.join(strArr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words = s.split(' ')\n",
    "        words.sort(key=lambda word: word[-1])\n",
    "\n",
    "        ans = ''\n",
    "        for word in words:\n",
    "            ans += word[:-1] + ' '\n",
    "        return ans[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        l = s.split(\" \")\n",
    "        res = [\"\" for _ in range(len(l))]\n",
    "        for c in l:\n",
    "            res[int(c[-1]) - 1] = c[:-1]\n",
    "        return \" \".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words = []\n",
    "        for i in range(1, 10):\n",
    "            for word in s.split(\" \"):\n",
    "                if int(word[-1]) == i:\n",
    "                    words.append(word[:-1])\n",
    "        return \" \".join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        list_s = s.split(\" \")\n",
    "        len_s = len(list_s)\n",
    "        temp_result = {}\n",
    "        for i in list_s:\n",
    "            temp_result[i[-1]] = i[0: len(i) - 1]\n",
    "        index = 0\n",
    "        result = []\n",
    "        while index < len_s:\n",
    "            result.append(temp_result[str(index + 1)])\n",
    "            index += 1\n",
    "        return \" \".join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        n = len(s)   # 单词数量\n",
    "        arr = [\"\" for _ in range(n)]   # 单词数组\n",
    "        for wd in s:\n",
    "            # 计算位置索引对应的单词数组下标，并将单词放入对应位置\n",
    "            # 数组下标为 0 开头，位置索引为 1 开头\n",
    "            arr[int(wd[-1])-1] = wd[:-1]\n",
    "        return \" \".join(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words = []\n",
    "        for i in range(1, 10):\n",
    "            for word in s.split(\" \"):\n",
    "                if int(word[-1]) == i:\n",
    "                    words.append(word[:-1])\n",
    "        return \" \".join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words=s.split(' ')\n",
    "        d={int(x[-1]):x[:-1] for x in words}\n",
    "        result=[]\n",
    "        for i in range(1,len(words)+1):\n",
    "            result.append(d[i])\n",
    "        return ' '.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        list = s.split()\n",
    "        list.sort(key=lambda x: x[-1])\n",
    "        str = \"\"\n",
    "        for word in list:\n",
    "            if word != list[-1]:\n",
    "                word = word.strip('0123456789')\n",
    "                str = str + word + \" \"\n",
    "            else:\n",
    "                word = word.strip('0123456789')\n",
    "                str = str + word\n",
    "        return str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split(' ')\n",
    "        m = []\n",
    "        for i in range(1, len(s)+1):\n",
    "            for j in s:\n",
    "                if j[-1] == str(i):\n",
    "                    m.append(j[:-1])\n",
    "                    break\n",
    "        return ' '.join(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words=s.split(' ')\n",
    "        d={int(x[-1]):x[:-1] for x in words}\n",
    "        #for word in words:\n",
    "        #    d[int(word[-1])]=word[:-1]\n",
    "        result=d[1]\n",
    "        for i in range(2,len(words)+1):\n",
    "            result+=' '+d[i]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words=s.split(' ')\n",
    "        d={int(x[-1]):x[:-1] for x in words}\n",
    "        result=[]\n",
    "        for i in range(1,len(words)+1):\n",
    "            result.append(d[i])\n",
    "        return ' '.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words=s.split(' ')\n",
    "        d={int(x[-1]):x[:-1] for x in words}\n",
    "        #for word in words:\n",
    "        #    d[int(word[-1])]=word[:-1]\n",
    "        result=d[1]\n",
    "        for i in range(2,len(words)+1):\n",
    "            result+=' '+d[i]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        my_list = s.split(\" \")\n",
    "        for i in range(len(my_list)-1):\n",
    "            exchange = False\n",
    "            for j in range(len(my_list)-i-1):\n",
    "                if my_list[j][-1] > my_list[j+1][-1]:\n",
    "                    my_list[j], my_list[j+1] = my_list[j+1], my_list[j]\n",
    "                    exchange = True\n",
    "            if not exchange:\n",
    "                break\n",
    "        for i in range(len(my_list)):\n",
    "            my_list[i] = my_list[i][:-1]\n",
    "        return ' '.join(my_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s_list = s.split(\" \")\n",
    "        r_list = [\"\"] * len(s_list)\n",
    "        for ss in s_list:\n",
    "            r_list[int(ss[-1]) - 1] = ss[:-1]\n",
    "        return \" \".join(r_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        result = s.split(' ')\n",
    "        result = [(word[:-1] , word[-1]) for word in result]\n",
    "        result.sort(key = lambda x : x[1])\n",
    "        return \" \".join([word[0] for word in result])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "\n",
    "        words = s.split(\" \")\n",
    "\n",
    "        n = len(words)\n",
    "        new_words = [\"\"]*n\n",
    "\n",
    "        for word in words:\n",
    "            index = int(word[-1])\n",
    "            new_words[index-1] = word[:-1]\n",
    "        return \" \".join(new_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words = s.split(\" \")\n",
    "        words.sort(key=lambda x:int(x[-1]))\n",
    "        ans = \"\"\n",
    "        for word in words:\n",
    "            n = len(word)\n",
    "            ans += word[:n-1]+\" \"\n",
    "        return ans[:-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        n = len(s)   # 单词数量\n",
    "        arr = [\"\" for _ in range(n)]   # 单词数组\n",
    "        for wd in s:\n",
    "            # 计算位置索引对应的单词数组下标，并将单词放入对应位置\n",
    "            # 数组下标为 0 开头，位置索引为 1 开头\n",
    "            arr[int(wd[-1])-1] = wd[:-1]\n",
    "        return \" \".join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        words = s.split()\n",
    "        words.sort(key = lambda x: x[-1])\n",
    "        for i, word in enumerate(words):\n",
    "            words[i] = word[:-1]\n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        str_list = s.split(\" \")\n",
    "        str_list.sort(key=lambda x: x[-1])\n",
    "        for i in range(len(str_list)):\n",
    "            str_list[i]=str_list[i][:-1]\n",
    "        \n",
    "        return ' '.join(str_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortSentence(self, s: str) -> str:\n",
    "        s = s.split()\n",
    "        res = [''] * len(s)\n",
    "        for i in s:\n",
    "            res[int(i[-1])-1] = i[:-1]\n",
    "        return ' '.join(res) "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
